/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.service.proxy;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.fast.config.IFastConfig;
import com.vtuzx.fast.dao.gallery.GalleryDao;
import com.vtuzx.fast.dao.setting.CharacterDao;
import com.vtuzx.fast.service.IFastService;
import com.vtuzx.fast.service.impl.FastCustomerServiceImpl;
import com.vtuzx.kingdee.xqc_core.dao.DepartmentDao;
import com.vtuzx.web.bean.TokenValueItem;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 用户服务
 */
@Service
public class XqcCustomerServiceImpl extends FastCustomerServiceImpl {

    @Autowired
    private IFastConfig fastConfig;
    @Autowired
    private IFastService fastService;
    @Autowired
    private CharacterDao characterDao;
    @Autowired
    private GalleryDao galleryDao;
    @Autowired
    private DepartmentDao departmentDao;

    public void createToken(List<TokenValueItem> tokenValueItemList, Document customer) {
        String department = VtuzxUtil.getString(customer, "department");
        String departmentName = "";
        if (!VtuzxUtil.isEmpty(department)) {
            Document depDoc = departmentDao.findOne(Criteria.where("fNumber").is(department));
            departmentName = VtuzxUtil.getString(depDoc, "fName");
        }
        tokenValueItemList.add(new TokenValueItem("nickName", VtuzxUtil.getObject(customer, "nickName"), false));
        tokenValueItemList.add(new TokenValueItem("departmentName", departmentName, false));
        tokenValueItemList.add(new TokenValueItem("department", VtuzxUtil.getObject(customer, "department"), false));
        tokenValueItemList.add(new TokenValueItem("gender", VtuzxUtil.getObject(customer, "gender"), false));
        tokenValueItemList.add(new TokenValueItem("birthday", VtuzxUtil.formatYmdF((Date)VtuzxUtil.getObject(customer, "birthday")), false));
        tokenValueItemList.add(new TokenValueItem("mobile", VtuzxUtil.getObject(customer, "mobile"), false));
        tokenValueItemList.add(new TokenValueItem("email", VtuzxUtil.getObject(customer, "email"), false));
        VtuzxMap resPowers = new VtuzxMap();
        VtuzxMap roleParams = new VtuzxMap();
        String avatar = VtuzxUtil.getString(customer, "avatar");
        if (!VtuzxUtil.isEmpty(avatar)) {
            tokenValueItemList.add(new TokenValueItem("avatar", this.fastConfig.rootUrl() + this.fastService.getAvatarUrl(avatar), false));
        }

        String avatarId = VtuzxUtil.getString(customer, "_avatarId");
        if (!VtuzxUtil.isEmpty(avatarId)) {
            Document avatarDoc = this.galleryDao.findById(avatarId);
            if (!VtuzxUtil.isEmpty(avatarDoc)) {
                tokenValueItemList.add(new TokenValueItem("avatarUrl", this.fastConfig.rootUrl() + this.fastConfig.workPath() + VtuzxUtil.getString(avatarDoc, "_from") + "/gallery/" + VtuzxUtil.getString(avatarDoc, "file"), false));
            }
        }

        List<ObjectId> _characterIdList = VtuzxUtil.getObject(customer, "_characterIds");
        if (_characterIdList != null && _characterIdList.size() > 0) {
            Iterator<Document> characterList = this.characterDao.findList(Criteria.where("_id").in(_characterIdList), (Document)null, 0, 0);
            List<String> characterKeyList = new ArrayList();

            boolean resIsSuperRole;
            Document character;
            for(resIsSuperRole = false; characterList.hasNext(); characterKeyList.add(VtuzxUtil.getString(character, "key"))) {
                character = (Document)characterList.next();
                if (VtuzxUtil.getBoolean(character, "isSuperRole")) {
                    resIsSuperRole = true;
                }

                Map<String, Object> pagePower = VtuzxUtil.getObject(character, "pagePower");
                Map<String, Object> roleParam = VtuzxUtil.getObject(character, "roleParam");
                if (!VtuzxUtil.isEmpty(pagePower)) {
                    pagePower.forEach(resPowers::append);
                }

                if (!VtuzxUtil.isEmpty(roleParam)) {
                    roleParam.forEach(roleParams::append);
                }
            }

            tokenValueItemList.add(new TokenValueItem("rolePower", roleParams, false));
            tokenValueItemList.add(new TokenValueItem("pagePower", resPowers, false));
            tokenValueItemList.add(new TokenValueItem("isSuperRole", resIsSuperRole, false));
            tokenValueItemList.add(new TokenValueItem("characterKeyList", characterKeyList, false));
        }

    }
}
